package com.atguigu.tingshu.user.service.impl;

import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.login.LoginClient;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.mapper.UserPaidTrackMapper;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
	@Resource
	private LoginClient loginClient;
	@Resource
	private UserPaidAlbumMapper userPaidAlbumMapper;
	@Resource
	private UserPaidTrackMapper userPaidTrackMapper;
//	@Transactional(rollbackFor = Exception.class)

	@GlobalTransactional
	@Override
	public Map wxLogin(String code,String type) {
		Map<String, Object> paramMap=new HashMap<>();
		paramMap.put("code",code);
		return loginClient.login(type,paramMap);
	}

	@Override
	public Boolean isPaidAlbum(Long albumId) {
		UserPaidAlbum userPaidAlbum = userPaidAlbumMapper.selectOne(new LambdaQueryWrapper<UserPaidAlbum>()
				.eq(UserPaidAlbum::getUserId, AuthContextHolder.getUserId())
				.eq(UserPaidAlbum::getAlbumId, albumId));
		return userPaidAlbum !=null;
	}

	@Override
	public List<UserPaidTrack> getPaidTrackList(Long albumId) {
		List<UserPaidTrack> userPaidTrackList = userPaidTrackMapper.selectList(new LambdaQueryWrapper<UserPaidTrack>().eq(UserPaidTrack::getUserId, AuthContextHolder.getUserId())
				.eq(UserPaidTrack::getAlbumId, albumId));
		return userPaidTrackList;
	}
}
